<?php
class cSearchReport extends BaseController{
	
	public function index(){
		
		$this->loadView("vMenu");
		$this->loadView("vSearchReport");
		$apps = DAO::getAll("Application"); 
		//Afficher la liste des applications
		Gui::Apps($apps);

		echo JsUtils::postFormAndBindTo("#deconnexion", "click", "cAccueil/deconnexion", "menu","#menu");
		echo JsUtils::postFormAndBindTo("#deconnexion", "click", "cAccueil/deconnexion", "menu","#menu");
		echo JsUtils::postAndBindTo("#appsSearch","change","cSearchReport/useCase","{id:$(this).val()}","#cas");
		
	}
	
	public function useCase(){
		
		if($_POST['id']!="aucune")
		{
			$idappli = $_POST['id'];
			$useCase = DAO::getAll("UseCase");
			$apps = DAO::getAll("Application");
			
			foreach($apps as $application){
				// Si une application correspond à l'id envoyé 
				if($application->getId()==$idappli){
					$appli=$application;
				}
			
			}
				
			$usecaseAppli= array();
			
			foreach($useCase as $useCases){
				if($useCases->getApplication()== $appli)
				{
					//On stocke tous les usecases d'une applic
					$usecaseAppli[]=$useCases;
						
				}
			}
			//Affiche les cas d'utilisation d'une appli
			Gui::useCases($usecaseAppli);		
		}
	}
		
	public function showCase(){
		
			
		if(!empty($_POST['id']) && $_POST['id'] != "rien" && isset($_SESSION['check']) && $_SESSION['check'] != ""){
			
			
			$_SESSION['idApps'] = $_POST['id'];
			$check = $_SESSION['check'];
			Gui::showReport($_POST['id'], $check);
		}
		elseif(!empty($_POST['id']) && $_POST['id'] != "rien"){
			
			$_SESSION['idApps'] = $_POST['id'];
			$_SESSION['check']= "";
			
			if(isset($_POST['check']) == "true"){
				$_SESSION['check'] = $check = 1;
			
				Gui::showReport($_POST['id'], $check);
			}
			else{
				Gui::showReport($_POST['id']);
			}
			
		}
		elseif (empty($_POST['id']) && $_POST['check'] == "true"){
			$_SESSION['check'] = $check = 1;
			
			Gui::showReport($_SESSION['idApps'], $check);
		}
		elseif (empty($_POST['id']) && $_POST['check'] == "false"){
			
			$_SESSION['check'] = " ";
			Gui::showReport($_SESSION['idApps']);
		}
		
		echo JsUtils::postAndBindTo(".ligne","click","cSearchReport/detail","{idReport:$(this).attr('value')}","#report");
		
	}
	
	
	public function detail(){
		if(isset($_POST['idReport'])){
			$idReport = $_POST['idReport'];
			Gui::message($idReport);
		}
		//echo JsUtils::postFormAndBindTo("#addMessageReport","click","cSearchReport/insertResponse", "formSubmitMessage","#response");
		echo JsUtils::postAndBindTo("#resolu","click","cSearchReport/resolvedReport","{idReportResolved:$(sendResolu).attr('value')}","#response");
		echo JsUtils::postAndBindTo("#addMessageReport","click","cSearchReport/insertResponse","{message:$('#message').val(),idReport:$('#idReport').attr('value')}","#report");
	}
	
	
	public function insertResponse(){
		// Contient id du report et celui de l'utilisateur
		if(!empty($_SESSION['id'])){
			$id = $_SESSION['id'];
		}
		$message = $_POST['message'];
		$idReport = $_POST['idReport'];
		
		$date = date("Y-m-d");
		
		$allMessage = DAO::getAll("Message");
			
			foreach ($allMessage as $messages){
		
				if($messages->getIdReport()==$idReport){
					$aMessage=true;
					
				}
			}
	
		
		$messageInsert = new Message($idReport,$id,$message,$date);
		$ret = DAO::insert($messageInsert);
		
		$reports = DAO::getAll("Report");
		
				
		foreach ($reports as $report){
			
			if($report->getId() == $idReport && isset($aMessage) == true){
			
				$report1 = new Report($report->getId(),"3",$report->getIdUtilisateur(),$report->getCode(),$report->getLibelle(),$report->getDescription(),$report->getCreationDate(),$report->getUserAction(),$report->getSystemMessage(),$report->getReponseOfficielle());
				DAO::update($report1);
				
			}
			elseif ($report->getId() == $idReport && isset($aMessage) != true){
				
				$report1 = new Report($report->getId(),"1",$report->getIdUtilisateur(),$report->getCode(),$report->getLibelle(),$report->getDescription(),$report->getCreationDate(),$report->getUserAction(),$report->getSystemMessage(),$report->getReponseOfficielle());
				DAO::update($report1);
			}
		}
		echo "<div id='report'>";
		Gui::message($idReport);
		echo "</div>";
		echo JsUtils::postAndBindTo("#addMessageReport","click","cSearchReport/insertResponse","{message:$('#message').val(),idReport:$('#idReport').attr('value')}","#report");
		
	}
	
	
	public function resolvedReport(){
		$idReportResolved = $_POST['idReportResolved'];
		$reports = DAO::getAll("Report");
		
		foreach ($reports as $report){
				
			if($report->getId() == $idReportResolved){
					
				$report1 = new Report($report->getId(),"5",$report->getIdUtilisateur(),$report->getCode(),$report->getLibelle(),$report->getDescription(),$report->getCreationDate(),$report->getUserAction(),$report->getSystemMessage(),$report->getReponseOfficielle());
				$ret = DAO::update($report1);
				
			}
		}
		
	}
	
	
}